<?php
require "../load.php";
error_reporting(0);
$action = $_POST["action"];
unset($_POST["action"]);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}

switch ($action) {
    case "add_note" :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        $content = $_POST["content"];
        if (empty($content)) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("You must input the note's content") . "</div>"
            );
        }
        else {
            $content = Owl_Format::esc_html($content);
            $owl_note = new Owl_Note();
            if ($owl_note->insert_notes(array("content" => $content))) {
                $data = array(
                    "code" => 1,
                    "message" => "<div class=\"ajax-success\">" . __("Insert note successful!") . "</div>"
                );
            }
            else {
                $data = array(
                    "code" => 0,
                    "message" => "<div class=\"ajax-error\">" . __("Error while insert note: ") . $owl_note->get_error() . "<div>"
                );
            }
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case "edit_note" :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        $content = $_POST["content"];
        $create_date = $_POST["create_date"];
        $sticky = ($_POST["sticky"] == "on") ? 1 : 0;
        if (empty($content)) {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("You must input the note's content") . "</div>"
            );
        }
        else {
            $content = Owl_Format::esc_html($content);
            $owl_note = new Owl_Note();
            if ($owl_note->update_notes($create_date, array("content" => $content, "sticky" => $sticky))) {
                $data = array(
                    "code" => 1,
                    "message" => "<div class=\"ajax-success\">" . __("Update note successful!") . "</div>"
                );
            }
            else {
                $data = array(
                    "code" => 0,
                    "message" => "<div class=\"ajax-error\">" . __("Error while update note: ") . $owl_note->get_error() . "<div>"
                );
            }
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    case 'delete_note' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        
        $owl_note = new Owl_Note();
        if ($owl_note->delete_notes($_POST["create_date"])) {
            $data = array(
                "code" => 1,
                "message" => "<div class=\"ajax-success\">" . __("Delete note successful!") . "</div>"
            );
        }
        else {
            $data = array(
                "code" => 0,
                "message" => "<div class=\"ajax-error\">" . __("Error while delete note: ") . $owl_note->get_error() . "<div>"
            );
        }
        $data = Owl_Functions::json_encode($data);
        echo $data;
        break;
    default:
        die();
        
}

die();
